﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ganosoft.Aspect;
using System.ServiceModel;
using System.Reflection;

namespace GanoESB.Component
{
    public class ExceptionInterceptor : IServiceInterceptor
    {
        public object Invoke(IServiceInvocation invocation)
        {
            try
            {
                return invocation.InvokeNext();
            }
            catch (Exception ex)
            {
                var faultExceptionType = typeof(FaultException<>);
                faultExceptionType = faultExceptionType.MakeGenericType(ex.GetType());
                ConstructorInfo ci = faultExceptionType.GetConstructor(new Type[] { ex.GetType(), typeof(FaultReason) });
                throw (FaultException)ci.Invoke(new object[] { ex, new FaultReason("服务调用失败.") });
            }
        }
    }
}
